var fs = require("fs");
var path = require("path");
var query = require("../db/db");


exports.userctrl = {
    jsonback: {
        success: '',
        msg: '',
        data: ''
    },
    add: function (data, callback) {
        var that = this;
        var sql1 = "select username from reg where username ='" + data.username + "'union all select username from user where username ='" + data.username + "';";
        var sql2 = "INSERT app.reg(username,password,mobile,bankusername,bankname,banknum,referrer,recommended) VALUES('" + data.username +
            "', '" + data.password + "', '" + data.mobile + "', '" + data.bankusername + "', '" + data.bankname + "', '" + data.banknum + "', '" + data.referrer + "', '" + data.recommended + "');";
        var sql3 = "INSERT app.user(username) VALUE('" + data.username + "');";
        query(sql1, function (err, vals, fields) {
            if (err) {
                that.jsonback.success = false;
                that.jsonback.msg = '查询注册账号是否存在时发生错误';
                that.jsonback.data = '';
                callback(that.jsonback);
                throw err;
            }
            if (!vals.length) {
                query(sql2, function (err, vals, fields) {
                    if (err) {
                        that.jsonback.success = false;
                        that.jsonback.msg = '写注册数据时发生错误1';
                        that.jsonback.data = '';
                        callback(that.jsonback);
                        throw err;
                    }
                    query(sql3, function (err, vals, fields) {
                        if (err) {
                            that.jsonback.success = false;
                            that.jsonback.msg = '写注册数据时发生错误2';
                            that.jsonback.data = '';
                            callback(that.jsonback);
                            throw err;
                        }
                        var sql4 = 'select * from reg where username = "' + data.username + '";';
                        query(sql4, function (err, vals, fields) {
                            if (err) {
                                that.jsonback.success = false;
                                that.jsonback.msg = '查询注册后数据出错';
                                that.jsonback.data = '';
                                callback(that.jsonback);
                                throw err;
                            }
                            if (vals.length) {
                                that.jsonback.success = true;
                                that.jsonback.msg = '注册成功';
                                that.jsonback.data = vals;
                                callback(that.jsonback)
                            } else {
                                that.jsonback.success = false;
                                that.jsonback.msg = '注册失败';
                                that.jsonback.data = vals;
                                callback(that.jsonback)
                            }
                        });
                    });

                })
            } else {
                that.jsonback.success = false;
                that.jsonback.msg = '该账号已被注册';
                that.jsonback.data = '';
                callback(that.jsonback);
            }
        });
    },
    login: function (data, callback) {
        var that = this;
        //查询是否存在
        var sql1 = "select * from reg where username ='" + data.username + "';";
        query(sql1, function (err, vals, fields) {
            if (err) {
                that.jsonback.success = false;
                that.jsonback.msg = '查询账号时发生错误1';
                that.jsonback.data = '';
                callback(that.jsonback);
                throw err;
            }
            if (vals.length) {
                if (vals[0].password == data.password) {
                    that.jsonback.success = true;
                    that.jsonback.msg = '登入验证成功';
                    that.jsonback.data = '';
                    callback(that.jsonback)
                } else {
                    that.jsonback.success = false;
                    that.jsonback.msg = '登入密码错误';
                    that.jsonback.data = '';
                    callback(that.jsonback)
                }
            } else {
                that.jsonback.success = false;
                that.jsonback.msg = '账号不存在';
                that.jsonback.data = '';
                callback(that.jsonback)
            }
        })
    },
    betview: function (req, callback) {
        var that = this;
        var user = req.session.winall;
        var page = req.query.page;
        var nums = req.query.nums;
        if (!nums) {
            nums = 10;
        }
        var sql1;
        if (page > 1) {
            sql1 = "select * from betlist where username ='" + user + "' order by time desc limit " + (page - 1) * nums + ',' + nums + ';';
        } else if (page == 1) {
            sql1 = "select * from betlist where username ='" + user + "' order by time desc limit 0," + nums + ';';
        } else {
            sql1 = "select * from betlist where username ='" + user + "' order by gameid desc;";
        }

        query(sql1, function (err, vals, fields) {
            if (err) {
                that.jsonback.success = false;
                that.jsonback.msg = '读取betlist数据库错误1';
                that.jsonback.data = '';
                callback(that.jsonback);
                throw err
            }
            if (vals.length) {
                that.jsonback.success = true;
                that.jsonback.msg = '读取成功';
                that.jsonback.data = vals;
                that.jsonback.totle = vals.length;
                callback(that.jsonback);
            } else {
                that.jsonback.success = false;
                that.jsonback.msg = '读取失败';
                that.jsonback.data = vals;
                callback(that.jsonback);
            }
        })
    },
    bet: function (req, callback) {
        var data = req.body;
        var user = req.session.winall;
        var that = this;
        var sql1 = "select reper,gamekind,gamekind2 from bettype where bettype = '" + data.bettype + "' and bettype2 ='" + data.bettype2 + "';";
        if (10 > data.money || data.money > 100000) {
            that.jsonback.success = false;
            that.jsonback.msg = '不在限额内';
            that.jsonback.data = '';
            callback(that.jsonback);
            return
        }
        query(sql1, function (err, vals, fields) {
            if (err) {
                that.jsonback.success = false;
                that.jsonback.msg = '查询赔率失败';
                that.jsonback.data = '';
                callback(that.jsonback);
                throw err;
            }
            if (!vals.length) {
                that.jsonback.success = false;
                that.jsonback.msg = '赔率不正确';
                that.jsonback.data = '';
                callback(that.jsonback);
            } else {
                var reper = vals[0].reper;//赔率
                var gamekind = vals[0].gamekind;//票种
                var gamekind2 = vals[0].gamekind2;//玩法
                var sql2 = 'select point from user where username = "' + user + '";';
                query(sql2, function (err, vals, fields) {
                    if (err) {
                        that.jsonback.success = false;
                        that.jsonback.msg = '查询余额失败';
                        that.jsonback.data = '';
                        callback(that.jsonback);
                        throw err;
                    }
                    var lastpoint = vals[0].point;//余额
                    if (lastpoint >= data.money) {
                        var sql3 = 'update user set point = point - ' + data.money + ' where username = "' + user + '"';
                        query(sql3, function (err, vals, fields) {
                            if (err) {
                                that.jsonback.success = false;
                                that.jsonback.msg = '扣钱失败';
                                that.jsonback.data = '';
                                callback(that.jsonback);
                                throw err;
                            }
                            var sql4 = 'insert app.betlist(username,gamekind,gamekind2,gameid,money,bettype,bettype2,ballbet,ballnum,reper) ' +
                                'values("' + user + '","' + gamekind + '","' + gamekind2 + '","' + data.bettype + data.gameid + '"' +
                                ',' + data.money + ',"' + data.bettype + '","' + data.bettype2 + '","' + data.ballbet + '","' + data.ballnum + '"' +
                                ',"' + reper + '");';
                            query(sql4, function (err, vals, fields) {
                                if (err) {
                                    that.jsonback.success = false;
                                    that.jsonback.msg = '下注失败,金额暂时冻结';
                                    that.jsonback.data = '';
                                    callback(that.jsonback);
                                    throw err;
                                }
                                var thisbetid = vals.insertId;
                                var sql5 = 'select * from betlist where id = ' + thisbetid + ';';
                                query(sql5, function (err, vals, fields) {
                                    if (err) {
                                        that.jsonback.success = false;
                                        that.jsonback.msg = '查询单注失败,金额暂时冻结';
                                        that.jsonback.data = '';
                                        callback(that.jsonback);
                                        throw err;
                                    }
                                    if (vals.length && vals[0].username == user && vals[0].gameid == data.bettype + data.gameid && vals[0].money == data.money) {
                                        that.jsonback.success = true;
                                        that.jsonback.msg = '下注成功';
                                        that.jsonback.data = vals;
                                        callback(that.jsonback);
                                    } else {
                                        var sql6 = 'update user set point = point + ' + data.money + ' where username = "' + user + '"';
                                        query(sql6, function (err, vals) {
                                            if (err) {
                                                that.jsonback.success = false;
                                                that.jsonback.msg = '下注失败,资金暂扣，稍后为您补回';
                                                that.jsonback.data = '';
                                                callback(that.jsonback);
                                                throw err;
                                            }
                                            that.jsonback.success = false;
                                            that.jsonback.msg = '下注失败,可能有浮点数存在';
                                            that.jsonback.data = '';
                                            callback(that.jsonback);
                                        })
                                    }
                                });
                            })
                        })
                    } else {
                        that.jsonback.success = false;
                        that.jsonback.msg = '余额不足';
                        that.jsonback.data = '';
                        callback(that.jsonback);
                    }
                })
            }
        });
    },
    loginstatus: function (user, callback) {
        var that = this;
        var sql1 = "select * from user where username ='" + user + "';";
        query(sql1, function (err, vals, fields) {
            if (err) {
                that.jsonback.code = 0;
                that.jsonback.success = false;
                that.jsonback.msg = '账号状态错误/账号';
                that.jsonback.data = '';
                callback(that.jsonback);
                throw err;
            }
            if (vals.length) {
                that.jsonback.code = 1;
                that.jsonback.success = true;
                that.jsonback.msg = '账号已登入';
                that.jsonback.data = vals;
                callback(that.jsonback);
            } else {
                that.jsonback.code = 0;
                that.jsonback.success = true;
                that.jsonback.msg = '账号验证错误';
                that.jsonback.data = vals;
                callback(that.jsonback);
            }
        });
    },
    edit: function (req, callback) {
        var that = this;
        var user = req.session.winall;
        var newpassword;
        var newmobile;
        var newbankusername;
        var newbankname;
        var newbanknum;
        var sql1 = 'select * from reg where username ="' + user + '";';
        query(sql1, function (err, vals) {
            if (err) {
                that.jsonback.success = false;
                that.jsonback.msg = '查询账号出错1';
                that.jsonback.data = '';
                callback(that.jsonback);
                throw err;
            }
            if (vals.length) {
                if (vals[0].password == req.body.password) {
                    req.body.newpassword ? newpassword = req.body.newpassword : newpassword = vals[0].password;
                    req.body.newmobile ? newmobile = req.body.newmobile : newmobile = vals[0].mobile;
                    req.body.newbankusername ? newbankusername = req.body.newbankusername : newbankusername = vals[0].bankusername;
                    req.body.newbankname ? newbankname = req.body.newbankname : newbankname = vals[0].bankname;
                    req.body.newbanknum ? newbanknum = req.body.newbanknum : newbanknum = vals[0].banknum;
                    var sql2 = 'update reg set password = "' + newpassword + '" ,mobile = "' + newmobile + '" ,' +
                        'bankusername = "' + newbankusername + '" ,bankname = "' + newbankname + '" ,banknum = "' + newbanknum + '" where username ="' + user + '"';
                    query(sql2, function (err, vals) {
                        if (err) {
                            that.jsonback.success = false;
                            that.jsonback.msg = '写表时出错';
                            that.jsonback.data = '';
                            callback(that.jsonback);
                            throw err;
                        }
                        if (!vals.length) {
                            var sql3 = 'select * from reg where username = "' + user + '";';
                            query(sql3, function (err, vals) {
                                if (vals.length) {
                                    if (err) {
                                        that.jsonback.success = false;
                                        that.jsonback.msg = '修改失败2';
                                        that.jsonback.data = '';
                                        callback(that.jsonback);
                                        throw err;
                                    }
                                    that.jsonback.success = true;
                                    that.jsonback.msg = '修改成功';
                                    that.jsonback.data = vals;
                                    callback(that.jsonback);
                                } else {
                                    that.jsonback.success = false;
                                    that.jsonback.msg = '修改失败3';
                                    that.jsonback.data = '';
                                    callback(that.jsonback);
                                }
                            })
                        } else {
                            that.jsonback.success = false;
                            that.jsonback.msg = '修改失败1';
                            that.jsonback.data = '';
                            callback(that.jsonback);
                        }
                    })
                } else {
                    that.jsonback.success = false;
                    that.jsonback.msg = '输入旧密码错误';
                    that.jsonback.data = '';
                    callback(that.jsonback);
                }
            } else {
                that.jsonback.success = false;
                that.jsonback.msg = '查询账号出错2';
                that.jsonback.data = '';
                callback(that.jsonback);
            }
        })
    }
};

